package com.example.demo.controller;


import com.example.demo.commom.Result;
import com.example.demo.entity.BookWithUser;
import com.example.demo.service.BookWithUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bookwithuser")
public class BookWithUserController {

    @Resource
    private BookWithUserService bookWithUserService;

    /**
     * 删除单条借阅记录：通过 id + isbn
     */
    @PostMapping("/deleteRecord")
    public Result<?> deleteRecord(@RequestBody BookWithUser record) {
        int result = bookWithUserService.deleteRecord(record);
        return result > 0 ? Result.success() : Result.error("400", "删除失败");
    }

    /**
     * 批量删除借阅记录
     */
    @PostMapping("/deleteRecords")
    public Result<?> deleteRecords(@RequestBody List<BookWithUser> list) {
        int successCount = 0;
        for (BookWithUser record : list) {
            int result = bookWithUserService.deleteRecord(record);
            if (result > 0) {
                successCount++;
            }
        }
        return successCount == list.size() ? Result.success() : Result.error("400", "部分删除失败");
    }

    /**
     * 延长借阅：prolong 字段 -1，并在 deadtime 上加 30 天
     */
    @PostMapping("/prolong")
    public Result<?> prolong(@RequestBody BookWithUser record) {
        int result = bookWithUserService.prolongRecord(record);
        return result > 0 ? Result.success() : Result.error("400", "续借失败");
    }

    /**
     * 修改借阅信息（图书名称 / 借阅者昵称 / 可续借次数）
     */
    @PostMapping("/update")
    public Result<?> updateRecord(@RequestBody BookWithUser record) {
        int result = bookWithUserService.updateRecord(record);
        return result > 0 ? Result.success() : Result.error("400", "更新失败");
    }

    /**
     * 分页查询，可按 ISBN、书名、ID 过滤
     */
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search1,
                              @RequestParam(defaultValue = "") String search2,
                              @RequestParam(defaultValue = "") String search3) {
        List<BookWithUser> records = bookWithUserService.selectPage(pageNum, pageSize, search1, search2, search3);
        int total = bookWithUserService.selectCount(search1, search2, search3);

        Map<String, Object> result = new HashMap<>();
        result.put("records", records);
        result.put("total", total);
        result.put("current", pageNum);
        result.put("size", pageSize);

        return Result.success(result);
    }

    @PostMapping("/insertNew")
    public Result<?> insertNew(@RequestBody BookWithUser rec) {
        int result = bookWithUserService.insert(rec);
        return result > 0 ? Result.success() : Result.error("400", "插入失败");
    }
}

